package com.fzy.core.system.entity;

import ge.core.system.entity.Entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.Constants;
import com.fzy.core.utils.BooleanUtils;
import com.fzy.core.utils.BuilderUtils;

public class EntityWrapper {
	Entity data;
	ModuleWrapper module;
	List properties;
	MappingWrapper mapping;
	
	public List getIntf() {
		List result = new ArrayList();
		if (getHasId()) {
			PropertyWrapper p = (PropertyWrapper) getKeyProperties().get(0);
			String peopertyType = p.getPropertyType();
			if (PropertyWrapper.STRING.equals(peopertyType))
				result.add("EntityIdString");
			if (PropertyWrapper.LONG.equals(peopertyType))
				result.add("EntityId");
		}

		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			String propertyName = p.getNameUncapital();
			String peopertyType = p.getPropertyType();

			if (p.getIsStatus()) {
				result.add("EntityStatus");
			}
			if (p.getIsCreatedUser()) {
				if (PropertyWrapper.STRING.equals(peopertyType))
					result.add("EntityCreatedUserString");
				if (PropertyWrapper.LONG.equals(peopertyType))
					result.add("EntityCreatedUser");
			}
			if (p.getIsCreatedDate()) {
				if (PropertyWrapper.STRING.equals(peopertyType))
					result.add("EntityCreatedDateString");
				if (PropertyWrapper.DATE.equals(peopertyType))
					result.add("EntityCreatedDate");
			}
			if (p.getIsUpdatedUser()) {
				if (PropertyWrapper.STRING.equals(peopertyType))
					result.add("EntityUpdatedUserString");
				if (PropertyWrapper.LONG.equals(peopertyType))
					result.add("EntityUpdatedUser");
			}
			if (p.getIsUpdatedDate()) {
				if (PropertyWrapper.STRING.equals(peopertyType))
					result.add("EntityUpdatedDateString");
				if (PropertyWrapper.DATE.equals(peopertyType))
					result.add("EntityUpdatedDate");
			}
		}
		return result;
	}
	
	public String getNameCapital() {
		return StringUtils.capitalize(data.getNameText());
	}

	public String getNameUncapital() {
		return StringUtils.uncapitalize(data.getNameText());
	}
	
	public boolean getHasPrefix(){
		return BooleanUtils.toBoolean(data.getHasPrefix(), true);
	}

	
	public boolean getIsWork(){
		return BooleanUtils.toBoolean(data.getIsWork(), true);
	}

	public String getPath() {
		String result = data.getPackageText();
		return result.replaceAll("\\.", Constants.SLASH);
	}

	public List getShowProperties() {
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if (p.getIsKey()) {
				result.add(p);
			}
		}
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if (!p.getIsKey() && p.getIsShow()) {
				result.add(p);
			}
		}
		return result;
	}
	
	public boolean getHasKey() {
		return getKeyProperties().size() > 0;
	}

	public List getKeyProperties() {
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if (p.getIsKey())
				result.add(p);
		}
		return result;
	}
	
	


	public boolean getHasId() {
		return getKeyProperties().size() == 1;
	}

	public boolean getHasCompositeId() {
		return getKeyProperties().size() > 1;
	}

	public PropertyWrapper getIdProperty() {
		return (PropertyWrapper) getKeyProperties().get(0);
	}

	public String getKeyColumns() {
		String result = "";
		for (Iterator iterator = getKeyProperties().iterator(); iterator
				.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if (result.length() < 1)
				result += p.getColumnName();
			else
				result += ", " + p.getColumnName();
		}
		return result;
	}


	//
	// public boolean getHasNumber(){
	// boolean result = false;
	// if(!result){
	// for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
	// PropertyWrapper p = (PropertyWrapper) iterator.next();
	// if(PropertyWrapper.NUMBER.equalsIgnoreCase(p.getData().getPropertyType())){
	// result = true;
	// break;
	// }
	// }
	// }
	// return result;
	// }

	public boolean getHasDate() {
		boolean result = false;
		if (!result) {
			for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if (PropertyWrapper.DATE.equalsIgnoreCase(p.getData()
						.getPropertyType())) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	/**
	 * vo显示的字段中，是否有DATE类型
	 * 
	 * @return
	 */
	public boolean getHasDateOfShow() {
		boolean result = false;
		if (!result) {
			for (Iterator iterator = getShowProperties().iterator(); iterator
					.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if (PropertyWrapper.DATE.equalsIgnoreCase(p.getData()
						.getPropertyType())) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	/**
	 * query查询的字段中，是否有DATE类型
	 * 
	 * @return
	 */
	public boolean getHasDateOfQuery() {
		boolean result = false;
		if (!result) {
			for (Iterator iterator = getQueryProperties().iterator(); iterator
					.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if (PropertyWrapper.DATE.equalsIgnoreCase(p.getData()
						.getPropertyType())) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	/**
	 * mapper显示的字段中，是否用到com.fzy.core.U类
	 * 
	 * @return
	 */
	public boolean getHasUtilsOfShow() {
		boolean result = false;
		if (!result) {
			for (Iterator iterator = getShowProperties().iterator(); iterator
					.hasNext();) {
				PropertyWrapper p = (PropertyWrapper) iterator.next();
				if ((PropertyWrapper.LONG.equalsIgnoreCase(p.getData()
						.getPropertyType()) || PropertyWrapper.NUMBER
						.equalsIgnoreCase(p.getData().getPropertyType()))) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public List getQueryProperties() {
		List result = new ArrayList();
		for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
			PropertyWrapper p = (PropertyWrapper) iterator.next();
			if (p.getIsQuery()) {
				result.add(p);
			}
		}
		return result;
	}
	
	public String getCommentText() {
		String result = "";
		String comment = data.getCommentText();
		if(StringUtils.isBlank(comment)) return result;
		comment = comment.trim();
		int firstParentheses = comment.indexOf("(");
		if(firstParentheses > -1){
			comment = comment.substring(0, firstParentheses);
		}
		if("0".equals(data.getIsWork())){
			comment += "(deleted:废弃)";
		}
		return comment;
	}


	// //////////////////////////////// generated
	// /////////////////////////////////////////////
	public Entity getData() {
		return data;
	}

	public void setData(Entity data) {
		this.data = data;
	}

	public List getProperties() {
		return properties;
	}

	public void setProperties(List properties) {
		this.properties = properties;
	}

	public ModuleWrapper getModule() {
		return module;
	}

	public void setModule(ModuleWrapper module) {
		this.module = module;
	}

	public MappingWrapper getMapping() {
		return mapping;
	}

	public void setMapping(MappingWrapper mapping) {
		this.mapping = mapping;
	}

}
